# ----------------------------------------------------------------------
# Replication for "Preventive Repression and Public Opinion"
# Katerina Tertytchnaya
# Forthcoming in World Politics (2023)
# ----------------------------------------------------------------------
# This code replicates the listed tables figures in the article and the appendix.
# It requires the packages listed below. If not already installed, please
# use the function install.packages() to install them.
# ----------------------------------------------------------------------

rm(list=ls())

library(stargazer)
library(ggplot2)
library(dotwhisker)
library(grid)
library(ggpubr)
library(foreign)
library(haven)
library(dplyr)
library(jtools) 
library(broom.mixed) 
library(gridExtra) 
library(MASS) 

## Set system locale
# "Sys.setlocale(,"ru_RU")"

# SET WORKING DIRECTORY
setwd("WP_Replication/")  
# ----------------------------------------------------------------------
## Load the datasets and run the code to set-up the data
# ----------------------------------------------------------------------
#September 2020 Experiment Replication Data
dat<-read.dta("Data/replication.dta") 
source("Code/experiment_setup.R")

# March 2020 Experiment Replication Data
dat2020<-read.dta("Data/omnibus_2020_march.dta") 
source("Code/march_survey_setup.R")

# Monthly Protest Data
mydata <- read.csv(file="Data/FigureA3.csv", header=TRUE, sep=",") 

dat2017<-read.dta("Data/omnibus_2017_june_missing.dta")

# ----------------------------------------------------------------------
## Manuscript
# ----------------------------------------------------------------------
# Table 1
# Run replicate_table_1.do

# Figure 1
source("Code/replicate_fig_1.R")

# Figure 2
source("Code/replicate_fig_2.R")
# For Stata code and output printed in Books, run replicate_experiment.do 

# Figure 3
source("Code/replicate_fig_3.R")
# For Stata code and output printed in Books, run replicate_experiment.do 

# ----------------------------------------------------------------------
## Appendix 
# ----------------------------------------------------------------------
# Figure A.1
source("Code/replicate_fig_A1.R")

# Figure A.2
source("Code/replicate_fig_A2.R")

# Figure A.3
source("Code/replicate_fig_A3.R")

# Table B.1 
# Run replicate_table_B1.do 

# Figure B.1 
# Run replicate_table_1.do

# Table C.1
source("Code/replicate_table_C1.R")

# Table C.2
source("Code/replicate_table_C2.R")

# Figure C.1
source("Code/replicate_fig_C1.R")

# Figure C.2 
# Run replicate_experiment.do

# Table C.3 
# Run replicate_experiment.do

# Table C.4 
# Run replicate_experiment.do

# Table C.5 
# Run replicate_experiment.do

# Figure C.3 
# Run replicate_experiment.do

# Table C.6
source("Code/replicate_table_C6.R")

# Figure C4
source("Code/replicate_fig_C4.R")

# Table C.7
source("Code/post_double_Selection.R")
source("Code/replicate_table_C7.R")

# Table D.1
source("Code/replicate_table_D1.R")

## END